package com.example.manu_facturer_orders.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manu_facturer_orders.mapper.CityMapper;
import com.example.manu_facturer_orders.mapper.OrderTableMapper;
import com.example.manu_facturer_orders.pojo.AllMonery;
import com.example.manu_facturer_orders.pojo.BrandVO;
import com.example.manu_facturer_orders.pojo.OrderTableVO;
import com.example.manu_facturer_orders.service.BrandService;
import com.example.manu_facturer_orders.service.OrderTableService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author: tobed
 * @date: 2022/9/11
 * @time: 19:20
 */
@Service
public class OrderTableServiceImpl implements OrderTableService {

    @Autowired
    OrderTableMapper orderTableMapper;

    @Autowired
    BrandService brandService;

    @Autowired
    CityMapper cityMapper;

    private Double Double;

    /**
     * 对应厂商订单全查+省市县查询
     * @param id
     * @param date
     * @param page
     * @return
     */
    @Override
    public Page<OrderTableVO> list(Integer id, String date, Page<OrderTableVO> page) {
        List<String> longs = new ArrayList<>();
        List<BrandVO> brandVOS = brandService.listByManuFactuerId(id);
        // 取出每一个值对应的品牌code
        for (int i = 0; i < brandVOS.size(); i++) {
            longs.add(brandVOS.get(i).getBrandCode());
        }
        Page<OrderTableVO> list = orderTableMapper.list(longs,date,page);
        return list;
    }

    @Override
    public Page<OrderTableVO> listByDate(String date, Page<OrderTableVO> page) {
        Page<OrderTableVO> list = orderTableMapper.listByDate(date,page);
        return list;
    }

    /**
     * 查询对应id所有订单
     * @param id
     * @param date
     * @return
     */
    @Override
    public void listByIdAndDate(Integer id, String date, HttpServletResponse response,XSSFWorkbook workbook) throws IOException {
        CellStyle cellstyle=workbook.createCellStyle();
        cellstyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        Font font=workbook.createFont();//字体
        font.setFontName("宋体");//字体
        font.setFontHeightInPoints((short)16);//字号
        font.setColor(IndexedColors.RED.getIndex());
        font.setBold(true);//加粗
        cellstyle.setFont(font);
        List<String> longs = new ArrayList<>();
        List<BrandVO> brandVOS = brandService.listByManuFactuerId(id);
        // 取出每一个值对应的品牌code
        for (int i = 0; i < brandVOS.size(); i++) {
          longs.add(brandVOS.get(i).getBrandCode());
        }
        List<OrderTableVO> list = orderTableMapper.listByIdAndDate(longs, date.substring(0,10));
        String fileName = new String("导出excel.xlsx".getBytes(), StandardCharsets.ISO_8859_1);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        ServletOutputStream outputStream = response.getOutputStream();
        Double SSS = 0.0;
        list.forEach(orderTableVO -> System.out.println(orderTableVO.getAmountPayable()));
        EasyExcel.write(outputStream, OrderTableVO.class)
                .sheet("sheet1")
                .doWrite(list);
    }



}
